//
//  OtherPeopleVC.m
//  MyProject
//
//  Created by jiaming yan on 2018/5/28.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "OtherPeopleVC.h"
#import "OtherPeopleTableViewCell.h"
#import "UITableView+Empty.h"
#import "EvaluationsHistory.h"
#import "OtherUserInfoModel.h"
#import "ReportOrCancelViewController.h"
#import "StringAndIndexModel.h"

@interface OtherPeopleVC ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic)  NSMutableArray *arrData;

@property (strong, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
@property (weak, nonatomic) IBOutlet UILabel *userNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *typeLabel;
@property (weak, nonatomic) IBOutlet UILabel *sendValueLabel;
@property (weak, nonatomic) IBOutlet UILabel *receiveValueLabel;
@property (weak, nonatomic) IBOutlet UILabel *comneyLabel;
@property (weak, nonatomic) IBOutlet UILabel *jobLabel;
@property (weak, nonatomic) IBOutlet UILabel *coutLabel;

@end

@implementation OtherPeopleVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"";
    [self rightButton];
    [self sendRequest];
    [self loadTabbleView];
    [self downRefreshRequest];
    [self upRefreshRequest];
}
-(void)rightButton{
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 100, 44);
    [backBtn setImage:IMAGE(@"ic_tip_black") forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(rightButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    negativeSpacer.width = -20;
    self.navigationItem.rightBarButtonItems = @[negativeSpacer,backItem];
    
    if (IOS11Later) {
        backBtn.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;
    }
}
-(void)rightButtonClick{
    NSArray *msgs = @[@"信息内容不真实", @"泄露/索取个人隐私", @"封建迷信、反动思想、涉黄涉暴等政治敏感内容", @"发布广告/虚假信息/传销等信息", @"辱骂/诽谤/恶意骚扰等人身攻击", @"其他"];
    NSMutableArray *dataArray = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < msgs.count; i++) {
        StringAndIndexModel *model = [[StringAndIndexModel alloc] init];
        if (i == 0) {
            model.index = 1;
        } else {
            model.index = 0;
        }
        model.msg = msgs[i];
        [dataArray addObject:model];
    }
    
    ReportOrCancelViewController *vc = [[ReportOrCancelViewController alloc] init];
    vc.index = 3;
    vc.dataArray = dataArray;
    vc.relatedId = self.user_id;
    vc.relatedType = @"REPORT_TYPE_CANDIDATE";
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
 方法描述:
 加载列表
 
 参数说明:
 <#参数说明#>
 
 返回结果:
 <#返回结果#>
 
 */
-(void)loadTabbleView{
    [self.tableView registerNib:[UINib nibWithNibName:@"OtherPeopleTableViewCell" bundle:nil] forCellReuseIdentifier:@"OtherPeopleTableViewCell"];
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.estimatedRowHeight = 184.0f;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
    }
    self.topView.frame = CGRectMake(0, 0, kScreenWidth, 212);
    self.tableView.tableHeaderView = self.topView;
}
-(void)sendRequest{
    __weak typeof(self) weakSelf = self;
    [self showProgressWithView:self.view animated:YES];
    [YJMSendRequest postDataWithParameters:@{@"userId":self.user_id} url:@"candidate/getCandidateInfo" result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        [self hideProgress:self.view animated:YES];
        if([result[@"result"] integerValue] == 1)
        {
            OtherUserInfoModel *model = [OtherUserInfoModel mj_objectWithKeyValues:result[@"data"]];
            
            [weakSelf.headImageView sd_setImageWithURL:[NSURL URLWithString:ImageURL(model.headImg)] placeholderImage:IMAGE(@"默认图")];
            weakSelf.userNameLabel.text = model.nickName;
            weakSelf.typeLabel.text = model.candidateStateName;
            weakSelf.sendValueLabel.text = [NSString stringWithFormat:@"%.0f%@",model.othersEvaluation*100,@"%"];
            weakSelf.receiveValueLabel.text = [NSString stringWithFormat:@"%.0f%@",model.receiveEvaluation*100,@"%"];
            weakSelf.comneyLabel.text = [NSString stringWithFormat:@"目标公司：%@",model.companyName];
            weakSelf.jobLabel.text = [NSString stringWithFormat:@"意向职位：%@",model.intentionalPositions];

                [weakSelf.tableView.mj_header beginRefreshing];
        }else{
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}
#pragma mark ------tableView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.arrData.count;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return 65;
//}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    static NSString *identifier = @"OtherPeopleTableViewCell";
    OtherPeopleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    //    MessageModel *model = self.arrData[indexPath.row];
    //    cell.titleName.text = model.title;
    EvaluationsHistory *model = self.arrData[indexPath.row];
    [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:ImageURL(model.publisherHeadImg)] placeholderImage:IMAGE(@"默认图")];
    cell.contentLabel.text = model.content;
    if (model.incumbentEvaluation.length > 0) {
        cell.returnLabel.text = [NSString stringWithFormat:@"同事回复：%@",model.incumbentEvaluation];
    }else{
        cell.returnLabel.text = @"";
    }
    
    cell.typeLabel.text = model.relatedServiceTypeName;
    

    return cell;
    
}


- (void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    
}
#pragma mark -
#pragma mark 下拉刷新
- (void)downRefreshRequest //内部方法改
{
    __weak typeof(self) weakSelf = self;
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        
        weakSelf.page = kStartPage;
        [YJMSendRequest postDataWithParameters:@{@"userId":self.user_id,@"pageSize":kNumOfPageSize,@"pageNo":[NSNumber numberWithInteger:weakSelf.page]} url:@"candidate/getCandidateEvaluations" result:^(NSDictionary *result, NSError *error) {
            result = [CommonUtils decryptDicWithResult:result];
            [self hideProgress:self.view animated:YES];
            if([result[@"result"] integerValue] == 1)
            {
                self.arrData = [NSMutableArray arrayWithArray:[EvaluationsHistory mj_objectArrayWithKeyValuesArray:result[@"data"][@"list"]]];
                weakSelf.coutLabel.text = [NSString stringWithFormat:@"历史评价 （%@）",result[@"data"][@"totalCount"]];
                if (weakSelf.arrData.count < kNumOfPageIntSize) {
                    [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
                    
                }else{
                    [weakSelf.tableView.mj_footer endRefreshing];
                }
                [weakSelf.tableView.mj_header endRefreshing];
                
            }else{
                [weakSelf.tableView.mj_header endRefreshing];
                [weakSelf.tableView.mj_footer endRefreshing];
                
                [CommonUtils showToast:[result objectForKey:@"msg"]];
            }
            if (self.arrData.count == 0) {
                [weakSelf.tableView noDataAddEmptyUIWithImageName:@"pic_emptystate_two" andEmptyText:@"暂无相关数据" andTopDistance:130];
                self.tableView.tableFooterView = nil;
            } else {
                [weakSelf.tableView haveDataRemoveEmptyUI];
            }
            [weakSelf.tableView reloadData];
        }];
    }];
    MJHEADER;
    
    
    self.tableView.mj_header = header;

}
#pragma mark 上拉加载
- (void)upRefreshRequest //内部方法改
{
    __weak typeof(self) weakSelf = self;
    
    MJRefreshBackGifFooter *footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        weakSelf.page ++;
        [YJMSendRequest postDataWithParameters:@{@"userId":self.user_id,@"pageSize":kNumOfPageSize,@"pageNo":[NSNumber numberWithInteger:weakSelf.page]} url:@"candidate/getCandidateEvaluations" result:^(NSDictionary *result, NSError *error) {
            result = [CommonUtils decryptDicWithResult:result];
            [self hideProgress:self.view animated:YES];
            if([result[@"result"] integerValue] == 1)
            {
                NSArray *arrTemp = [EvaluationsHistory mj_objectArrayWithKeyValuesArray:result[@"data"][@"list"]];
                [self.arrData addObjectsFromArray:arrTemp];
                if (arrTemp.count < kNumOfPageIntSize) {
                    [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];

                }else{
                    [weakSelf.tableView.mj_footer endRefreshing];
                }
                [weakSelf.tableView.mj_header endRefreshing];
                
            }else{
                [weakSelf.tableView.mj_header endRefreshing];
                [weakSelf.tableView.mj_footer endRefreshing];
                
                [CommonUtils showToast:[result objectForKey:@"msg"]];
            }
            if (self.arrData.count == 0) {
                [weakSelf.tableView noDataAddEmptyUIWithImageName:@"pic_emptystate_two" andEmptyText:@"暂无相关数据" andTopDistance:130];
                self.tableView.tableFooterView = nil;
            } else {
                [weakSelf.tableView haveDataRemoveEmptyUI];
            }
            [weakSelf.tableView reloadData];
        }];
    }];
    
    MJFOOTER;
    self.tableView.mj_footer = footer;
    
    
}
- (IBAction)sendMessageClick:(id)sender {
    __weak typeof(self) weakSelf = self;
    [self showProgressWithView:self.view animated:YES];
    [YJMSendRequest postDataWithParameters:@{@"userId":self.user_id} url:@"incumbent/submitBusinessCard" result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        [self hideProgress:self.view animated:YES];
        if([result[@"result"] integerValue] == 1)
        {
            [[CommonUtils sharedCommonUtils]showAlert:@"名片发送成功" delegate:nil];
        }else{
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
